/*
 * @Author: qiuz
 * @Date: 2018-07-05 13:19:01
 * */

export default function detectOS(version = false) {
	const sUserAgent = navigator.userAgent;

	const	isMac = (navigator.platform === 'Mac68K') || (navigator.platform === 'MacPPC') || (navigator.platform === 'Macintosh') || (navigator.platform === 'MacIntel');
	if (isMac) return 'Mac';

	const	isWin = (navigator.platform === 'Win32') || (navigator.platform === 'Windows');
	if (isWin && !version) return 'Win';
	if (isWin && version) {
		const isWin2K = sUserAgent.indexOf('Windows NT 5.0') > -1 || sUserAgent.indexOf('Windows 2000') > -1;
		if (isWin2K) return 'Win2000';
		const isWinXP = sUserAgent.indexOf('Windows NT 5.1') > -1 || sUserAgent.indexOf('Windows XP') > -1;
		if (isWinXP) return 'WinXP';
		const isWin2003 = sUserAgent.indexOf('Windows NT 5.2') > -1 || sUserAgent.indexOf('Windows 2003') > -1;
		if (isWin2003) return 'Win2003';
		const isWinVista = sUserAgent.indexOf('Windows NT 6.0') > -1 || sUserAgent.indexOf('Windows Vista') > -1;
		if (isWinVista) return 'WinVista';
		const isWin7 = sUserAgent.indexOf('Windows NT 6.1') > -1 || sUserAgent.indexOf('Windows 7') > -1;
		if (isWin7) return 'Win7';
		const isWin8 = sUserAgent.indexOf('Windows NT 6.2') > -1 || sUserAgent.indexOf('Windows 8') > -1;
		if (isWin8) return 'Win8';
		const isWin8_1 = sUserAgent.indexOf('Windows NT 6.3') > -1 || sUserAgent.indexOf('Windows 8.1') > -1;
		if (isWin8_1) return 'Win8.1';
		const isWin10 = sUserAgent.indexOf('Windows NT 6.4') > -1 || sUserAgent.indexOf('Windows 10') > -1;
		if (isWin10) return 'Win10';
		return 'Win';
	}

	const isUnix = (navigator.platform === 'X11') && !isWin && !isMac;
	if (isUnix) return 'Unix';
	const isLinux = (String(navigator.platform).indexOf('Linux') > -1);
	const bIsAndroid = sUserAgent.toLowerCase().match(/android/i) === 'android';
	if (isLinux) {
		if (bIsAndroid) return 'Android';
		else return 'Linux';
	}

	const isiOS = !!sUserAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
	if (isiOS) return 'iOS';

	return 'other';
};
